package com.kejiguan.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kejiguan.ui.adapter.NeItemRunDataItemAdapter;
import com.kejiguan.ui.presenter.NeItemRunDataPresenter;
import com.kejiguan.ui.response.NeItemRunDataResponse;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
/**
 * Created by YYY on 2016/4/28.
 */
public class NeItemRunDataFragment extends Fragment {

    private NeItemRunDataPresenter mPresenter;

    private View rootView;
    private RecyclerView neItemRunDataList;
    private NeItemRunDataItemAdapter itemAdapter;

    private int currentPosition = 0;


    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        EventBus.getDefault().register(this);
        mPresenter = new NeItemRunDataPresenter();
        mPresenter.attach(getContext());
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
        if (rootView == null) {

            rootView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_neitem_rundata_layout, container, false);
            neItemRunDataList = (RecyclerView) rootView.findViewById(R.id.display_item_list);

            LinearLayoutManager displayAreaLayoutManager = new LinearLayoutManager(getContext());
            displayAreaLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);


            LinearLayoutManager itemListLayoutManager = new LinearLayoutManager(getContext());
            itemListLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            neItemRunDataList.setLayoutManager(itemListLayoutManager);
            itemAdapter = new NeItemRunDataItemAdapter(getContext(), null);
            neItemRunDataList.setAdapter(itemAdapter);

            mPresenter.getNeItemRunData(getContext(), "", "");


        }
        return rootView;
    }

    public void reLoad()
    {
        if (null!=mPresenter)
        {
            mPresenter.getNeItemRunData(getContext(),"","");
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(NeItemRunDataResponse response) {

        if ("0000".equals(response.getRetCode()))
        {
            itemAdapter.setNeItemRunItems(response.getNeItemRunItems());
        }
    }


}
